package com.handu.luna.core.domain.promotion;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.criteria.*;
import javax.validation.constraints.Size;

import com.handu.luna.core.domain.search.CreditGoodsSearch;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;

@Entity
public class CreditGoods extends Promotion {

	@Size(min=1,max=100, message="最多100个字")
	private String creditGoodsName;

    @Override
    public String getStatus() {
        return initStatus(this.getStartTime(), this.getEndTime());
    }


    public enum CreditType {

		CREDIT("全额积分兑换"), BOTH("积分加现金购"), PRIZE("积分好礼");
		public String description;


        CreditType(final String description) {
			this.description = description;
		}
	}

    private CreditType creditType;
	private String[] userRank;
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	private Date startTime;
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	private Date endTime;

    public static Specification<CreditGoods> selectCondition(CreditGoodsSearch creditGoodsSearch) {

        return new Specification<CreditGoods>() {
			@Override
			public Predicate toPredicate(Root<CreditGoods> root,
					CriteriaQuery<?> query, CriteriaBuilder builder) {

				Predicate predicate = builder.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();

				if (StringUtils.isNotBlank(creditGoodsSearch.getName())) {
					expressions.add(builder.like(root.<String> get("creditGoodsName"),"%"+creditGoodsSearch.getName() +"%"));
				}

				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date date1 = null;
				Date date2 = null;

				if(StringUtils.isNotBlank(creditGoodsSearch.getStartDate())){
					try {
						date1 = sdf.parse(creditGoodsSearch.getStartDate());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					expressions.add(builder.greaterThanOrEqualTo(root.<Date>get("startTime"), date1));
				}

				if(StringUtils.isNotBlank(creditGoodsSearch.getEndDate())){
					try {
						date2 = sdf.parse(creditGoodsSearch.getEndDate());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					expressions.add(builder.lessThanOrEqualTo(root.<Date>get("startTime"), date2));
				}

                if(StringUtils.isNotBlank(creditGoodsSearch.getStatus())) {
                   if(creditGoodsSearch.getStatus().equals(PromotionStatus.UNACTIVATED.description)) {
                       expressions.add(builder.greaterThan(root.<Date>get("startTime"), new Date()));
                   }else if(creditGoodsSearch.getStatus().equals(PromotionStatus.ACTIVATING.description)) {
                       expressions.add(builder.lessThan(root.<Date>get("startTime"), new Date()));
                       expressions.add(builder.greaterThan(root.<Date>get("endTime"), new Date()));
                   }else if(creditGoodsSearch.getStatus().equals(PromotionStatus.ACTIVATED.description)){
                       expressions.add(builder.lessThan(root.<Date>get("endTime"), new Date()));
                   }
                }

				return query.where(predicate).getRestriction();

			}
		};
    }

	public String getCreditGoodsName() {
		return creditGoodsName;
	}
	public void setCreditGoodsName(String creditGoodsName) {
		this.creditGoodsName = creditGoodsName;
	}
	public Date getStartTime() {
		return startTime;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public Date getEndTime() {
		return endTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	public String[] getUserRank() {
		return userRank;
	}
	public void setUserRank(String[] userRank) {
		this.userRank = userRank;
	}

    public CreditType getCreditType() {
        return creditType;
    }

    public void setCreditType(CreditType creditType) {
        this.creditType = creditType;
    }
}
